const path = require("path");
const webpack = require('webpack');
const {BundleAnalyzerPlugin} = require("webpack-bundle-analyzer");
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const plugins = [
    new FriendlyErrorsWebpackPlugin(),
    new MiniCssExtractPlugin({
        filename: "[name].[hash].css",
        chunkFilename: '[id].[hash].css'
    })
];

plugins.push(new BundleAnalyzerPlugin({
    analyzerMode: "static",
    reportFilename: "build-report.html",
    openAnalyzer: false,
}));
plugins.push(
    new HtmlWebPackPlugin({
        title: '主页 - 大数据平台 - 惠生集团',
        template: "./client/index.html",
        filename: "index.html"
    }));
plugins.push(new webpack.DefinePlugin({
    __isClient__: true
}));

let currentPath = process.cwd();

module.exports = {
    mode: "production",
    context: path.join(currentPath, "client", "src"),
    devtool: "source-map",
    optimization: {
        splitChunks: {
            chunks: 'all',
            minSize: 1024 * 256,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            automaticNameDelimiter: '.',
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10
                },
            }
        }
    },
    entry: {
        app: [
            "whatwg-fetch",
            "babel-polyfill",
            path.join(currentPath, "client/src/client.js")
        ]
    },
    resolve: {
        modules: [
            path.resolve("./client"),
            "node_modules",
        ],
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['react', 'es2015', 'stage-0']
                }
            },
            {
                test: /(\.css)$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    {
                        loader: 'css-loader'
                    },
                ]
            },
            {
                test: /\.less$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    {
                        loader: 'css-loader'
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            strictMath: true,
                            noIeCompat: true,
                            sourceMap: false,
                            javascriptEnabled: true
                        }
                    }
                ],
            },
            {
                test: /\.(png|jpg|gif|mp4)$/i,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: "[hash].[ext]",
                            publicPath: "/static",
                        }
                    }
                ]
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: "[hash].[ext]",
                            publicPath: "/static",
                        }
                    }]
            },
            {
                test: /\.(ttf|eot|svg|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: "[hash].[ext]",
                            publicPath: "/static",
                        }
                    }]
            }
        ],
    },
    output: {
        path: path.resolve(process.cwd(), "static"),
        filename: "[name].[hash].js",
        publicPath: "static/",
        chunkFilename: "[name].[hash].js"
    },
    plugins,
};
